// pages/problem_feedback/problem_feedback.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['留言','投诉','询问','售后'],
    msg_type:0,
    goods_id:0,
    message_img:'',
    msg_title:''
  },
  // 去问题反馈列表
  go_list:function(){
    wx.navigateTo({
      url: '../fan_list/fan_list',
    })
  },

  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      msg_type: e.detail.value
    })
  },
  msg_title_change:function(e){
    this.setData({
      msg_title: e.detail.value
    })
  },
  setimg:function(){
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },
  chooseWxImage: function (type) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res);
        that.setData({
          message_img: res.tempFilePaths[0],
        })
      }
    })
  },
  deleteImg: function (e) {
  
    this.setData({
      message_img: ""
    });
  },
  submit:function(e){
    var that = this;
    if (that.data.msg_title == ""){
      wx.showToast({
        title: '请填写补充说明',
        icon: 'none',
        mask: true,
      })
      return false
    }
    if(!that.data.message_img){
      wx.showToast({
        title: '请选择图片',
        icon: 'none',
        mask: true,
      })
      return false
    }
    var msg_title = that.data.msg_title,
      msg_type = that.data.msg_type,
    message_img = that.data.message_img,
    openid = app.globalData.opid;
    console.log(msg_title)
    console.log(message_img)
    console.log(msg_type)
    console.log(openid)
  
    wx.uploadFile({
      url: app.globalData.url +'ziyu.php?Action=shangquanpic', 
      filePath: message_img,
      name: 'pic',
      
      success: function (res) {
        console.log(res)
        var data = JSON.parse(res.data);
        console.log(data)
        wx.request({
          url: app.globalData.url + 'ziyu.php?Action=wentifankui',
          data: {
            goods_id: that.data.id,
            msg_title: msg_title,
            msg_type: msg_type,
            openid: openid,
            pic: data.pic
          },
          success: function (res) {
            console.log(res)
            wx.showToast({
              title: '提交成功',
              icon: 'none',
              mask: true,
            })
            setTimeout(function(){
              wx.navigateTo({
                url: '../fan_list/fan_list',
              })
            },1000)

          }
        })
       
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.setData({
    //   msg_title_change: "",
    //   message_img: ''
      
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})